package com.hh.bss.project.controller;
import static com.hh.bss.util.extjs.JsonHelper.outJson;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.hh.bss.auth.client.OnlineManager;
import com.hh.bss.auth.client.OnlineUser;
import com.hh.bss.base.BaseSpringController;
import com.hh.bss.common.page.Page;
import com.hh.bss.common.page.PageRequest;
import com.hh.bss.project.entity.AccountInvoice;
import com.hh.bss.project.entity.AccountPnAttachment;
import com.hh.bss.project.service.AccountPnAttachmentService;
import com.hh.bss.project.service.AccountPnService;
import com.hh.bss.project.vo.query.AccountPnAttachmentQuery;
import com.hh.bss.util.extjs.ExtJsPageHelper;
import com.hh.bss.util.extjs.ListRange;

@Controller
public class AccountPnAttachmentController extends BaseSpringController {
	
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private AccountPnAttachmentService  accountPnAttachmentService;
	
	private AccountPnAttachment accountPnAttachment;
	
	private Integer id;
	
	private String[] items;
	
	/** 通过spring自动注入 */
	public void setAccountPnAttachmentServiceImpl(AccountPnAttachmentService service) {
		this.accountPnAttachmentService = service;
	}	
	
	public void list(HttpServletRequest request,HttpServletResponse response,AccountPnAttachmentQuery attachmentQuery) throws IOException
	{
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		ListRange<AccountPnAttachment> resultList=new ListRange();
		
		try{
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if (onlineManager!=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			attachmentQuery.setOnlineUser(onlineUser);
		}	
		Page page = accountPnAttachmentService.findPage(attachmentQuery);
		
		List<AccountPnAttachment> pnAttachmentsList = (List) page.getResult();
		resultList = new ListRange<AccountPnAttachment>();
		resultList.setRows(pnAttachmentsList);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");		
		resultList.setSuccess(true);
		}
		catch (Exception e)
		{
			resultList.setMessage("系统异常");		
			resultList.setSuccess(false);
			e.printStackTrace();
		}
				
		outJson(response,resultList);
		
	}
	public void get(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		String id = request.getParameter("id");
		if (StringUtils.isNotEmpty(id)) {
			result.put("failure", true);
			result.put("msg", "参数非法!");
		}
		try {
			AccountPnAttachment data = (AccountPnAttachment) accountPnAttachmentService.getById(Integer.parseInt(id));
			if (data != null) {
				result.put("data", data);
				result.put("success", true);
				result.put("msg", "成功!");
			}

		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}
	 public String managerPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
			
			return "forward:/jsp/project/accountPnAttachment/accountPnAttachmentPage.jsp";
			
     }
	
	public void save(HttpServletRequest request,HttpServletResponse response,AccountPnAttachment attachment) throws IOException
	{
		
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        //设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(AccountPnAttachment.class,"creator")) {
						try{com.hh.bss.util.BeanUtils.setProperty(attachment,"creator",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(AccountPnAttachment.class,"createDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(attachment,"createDt",new Date());}catch(Exception ex){}
				}

			//处理文件上传开始.
			MultipartHttpServletRequest multipartRequest=null;
			MultipartFile multipartFile =null;
			
			try {
				multipartRequest = (MultipartHttpServletRequest)request;
				multipartFile = multipartRequest.getFile("uploadFile");
			} catch (Exception ex) {
				ex.printStackTrace();
				result.put("failure", true);
				result.put("msg", ex.getMessage());
				outJson(response,result);
				return;
			}

		       if (multipartRequest!=null&&multipartFile!=null&&!multipartFile.isEmpty()){
		    	   accountPnAttachmentService.saveFile(multipartRequest, request, attachment);		    	   
		       }
		       
			accountPnAttachmentService.save(attachment);
			result.put("success", true);
			result.put("msg", "添加成功!");
		}
		catch (Exception e)
		{
			e.printStackTrace();
			result.put("failure", true);
			result.put("msg", e.getMessage());
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid删除
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		String ids = request.getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			for (int i = 0; i < idarray.length; i++)
			{
				java.lang.Integer id = new java.lang.Integer((String)idarray[i]);
				accountPnAttachmentService.removeById(id);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String[] getItems() {
		return items;
	}

	public void setItems(String[] items) {
		this.items = items;
	}
	
	
	

}
